package com.wuseguang.springcloud.thread;

import com.wuseguang.springcloud.JdbcTemplate.HaiweidaCooperativeSchoolJdbc;
import com.wuseguang.springcloud.dao.jdbc.StudentFacesTwoJdbc;
import com.wuseguang.springcloud.dao.repository.StudentFacesRegistrationRepository;
import com.wuseguang.springcloud.dao.repository.StudentFacesTwoRepository;
import com.wuseguang.springcloud.entity.StudentFacesTwo;
import com.wuseguang.springcloud.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class FacePackThread implements Runnable {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    String uploadFileName;
    String schoolId;

    public FacePackThread(String uploadFileName, String schoolId) {
        this.uploadFileName = uploadFileName;
        this.schoolId = schoolId;
    }

    public FacePackThread() {

    }

    private StudentFacesTwoJdbc studentFacesTwoJdbc;

    private StudentFacesTwoRepository studentFacesTwoRepository;

    private StudentFacesRegistrationRepository studentFacesRegistrationRepository;

    private HaiweidaCooperativeSchoolJdbc haiweidaCooperativeSchoolJdbc;

    @Override
    public void run() {

        //人脸集合
        List<Map<String, Object>> list = new ArrayList<>();
        logger.info("线程开始时间");

        studentFacesTwoJdbc = SpringBeanUtil.getBean(StudentFacesTwoJdbc.class);
        studentFacesTwoRepository = SpringBeanUtil.getBean(StudentFacesTwoRepository.class);
        studentFacesRegistrationRepository = SpringBeanUtil.getBean(StudentFacesRegistrationRepository.class);
        haiweidaCooperativeSchoolJdbc = SpringBeanUtil.getBean(HaiweidaCooperativeSchoolJdbc.class);

        //删除所有临时文件
        File file2 = new File(uploadFileName);

        try {
            //上传图片到oss
            Map<String, String> map = ZipUncompress.getPicture(file2, schoolId);

            //循环map结合
            for (Map.Entry<String, String> entry : map.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                System.out.println(key + ":" + value);
                //第一个班级名称第二个是学生名称
                String name[] = key.split("/");
                //查询班级sql
                String sql = "select id string from classes where name = ? and school_id = ?";
                //班级id
                String classId = studentFacesTwoJdbc.queryString(sql, name[0], schoolId);
                //查询学生
                sql = "select id string from student where student_name = ? and class_id = ?";
                //学生id
                String studentId = studentFacesTwoJdbc.queryString(sql, name[1], classId);

                //如果学生为空就退出
                if(StringUtils.isEmpty(studentId)){
                    continue;
                }

                //人俩Map
                Map<String, Object> facesMap = new HashMap<>();
                facesMap.put("userId", studentId);
                facesMap.put("photoUrl", value);
                facesMap.put("type", "student");
                list.add(facesMap);


                //执行人脸操作
                StudentFacesTwo studentFaces = studentFacesTwoRepository.findByStudentId(Integer.valueOf(studentId));
                if (!StringUtils.isEmpty(studentFaces)) {

                    //判断是否还有人脸
                    if (studentFacesTwoJdbc.judgeStudentFaces(studentFaces.getId())) {
                        continue;
                    }
                    studentFacesRegistrationRepository.deleteByStudentFacesId(studentFaces.getId());
                }else {
                    //人脸为空创建
                    studentFaces = new StudentFacesTwo();
                    studentFaces.setSchoolId(Integer.valueOf(schoolId));
                }
                studentFaces.setStudentId(Integer.valueOf(studentId));
                studentFaces.setUrl(value);

                logger.info("添加学生人脸");
                //学生上传的人脸
                Integer id = studentFacesTwoRepository.save(studentFaces).getId();
                studentFacesTwoJdbc.insertClassStudentFaces(id, Integer.valueOf(schoolId));
            }

            haiweidaCooperativeSchoolJdbc.haiweidaStudentFacesTwo(list, schoolId);
        } catch (Exception e) {
            logger.error("e", e);
        }


        ZipUncompress.deleteFile(file2);
    }

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    public String getSchoolId() {
        return schoolId;
    }

    public void setSchoolId(String schoolId) {
        this.schoolId = schoolId;
    }
}
